<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests\admin\StoreCateRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Org\Page;

class CateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

	//浏览商品分类
    public function index()
    {
       

        $list = \DB::select("select * from cate order by concat(path,id) asc ");
        
       

        //处理信息
        foreach($list as &$v){
            $m = substr_count($v->path,","); //获取path中的逗号
           
            //生成缩进
            $v->name = str_repeat("&nbsp;",($m-1)*8)."|--".$v->name;
           
        }   
        return view("admin.cate.index",['list'=>$list
                                              
                                         ]);
    }
   

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
	 
	//执行加载分类页面
		public function create()
    {	
		$list=\DB::table("cate")->get();
        return view("Admin.cate.create",['list'=>$list,
                                           
                                            ]);
    }
	
	
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
	 
	//执行添加
    public function store(Request $request)
    {   
      
		//获取要添加的数据
		$data=$request->all();		
		unset($data['_token']);

        //表验证
       if($data['name']==null){
            return redirect("admin/cate")->with("err","父类名称不能为空！");
       }
		//var_dump($data);
		//判断当前是否为等级分类
        if($data['pid']==0){
            $data['path']='0,';
        }else{
            //读取父级分类
            $p=\DB::table("cate")->where('id',$data['pid'])->first();//获取一行数据
            //拼接path路径
            $data['path']=$p->path.$p->id.',';
        }

		$m=\DB::table("cate")->insertGetID($data);
		
		//判段执行错误信息输出
       if($m>0){
        $info="添加成功！";
       }else{
        $info="添加失败！";
       }
       return redirect("admin/cate")->with("err",$info);
		
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

    //加载编辑页面
    public function show($id)
    {   //获取cate数据表中所有数据
       
      $list=\DB::table("cate")->where('id',$id)->first();
      $info=\DB::table("cate")->get();
      //s($list);
      //dd($info); 
        return view("admin.cate.edit",['list'=>$list,
                                        "info"=>$info
                                        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
       //return 122;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {  
  
       //获取参数
        $data = $request->only(['pid','name','status']);
        
        //表验证
       if($data['name']==null){
            return redirect("admin/cate")->with("err","父类名称不能为空！");
       }

        $id=$request->input('id');
        $pid=$request->input('pid');
        //获取父级数据
        $list=\DB::table("cate")->where('id',$pid)->first();
       
       //判断当前是否为等级分类
        if($data['pid']==0){
            $data['path']='0,';
        }else{
            //读取父级分类
            $p=\DB::table("cate")->where('id',$data['pid'])->first();//获取一行数据
            //拼接path路径
            $data['path']=$p->path.$p->id.',';
        }
        //更新动作
        if(\DB::table('cate')->where('id',$id)->update($data)){
         return $this->index();
        }else{
            return back()->with('error',"编辑失败,重新编辑!");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
         
         //统计当前商品下是否含有子类的总数
        $m=\DB::table("cate")->where('pid',$id)->count();
        
        //判段当前商品下是否含有子类
        if($m>0){
            return back()->with("err","该类下有子类禁止删除！");
        }else{
            \DB::table('cate')->delete($id);
            return redirect('admin/cate')->with("err","删除成功！");
        }

    }
}
